package cn.com.startai.smartadh5.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.startai.smartadh5.R;
import cn.com.startai.smartadh5.extension.utils.TimerUtils;
import cn.com.startai.smartadh5.extension.utils.homedata.HomeDataUtils;
import cn.com.startai.smartadh5.main.adapter.ShowFileListAdapter;
import cn.com.startai.smartadh5.main.base.activity.BaseActivityV1;
import cn.com.startai.smartadh5.processlogic.entity.bean.DeviceInfoBean;
import cn.com.startai.smartadh5.processlogic.entity.bean.ShowFileTypeBean;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 用于手机端控制设备进行音乐、文件等跳转
 **/
@SuppressWarnings("all")
public class PhoneControlActivity extends BaseActivityV1 {


    @BindView(R.id.fragment_date)
    TextView fragmentDate;
    @BindView(R.id.fragment_morning)
    TextView fragmentMorning;
    @BindView(R.id.fragment_time)
    TextView fragmentTime;
    @BindView(R.id.fragment_week)
    TextView fragmentWeek;
    @BindView(R.id.fragment_areas)
    TextView fragmentAreas;
    @BindView(R.id.wifi_show_state)
    ImageView wifiShowState;
    @BindView(R.id.wifi_ssid)
    TextView wifiSsid;
    @BindView(R.id.battery_state)
    ImageView batteryState;
    @BindView(R.id.batter_number)
    TextView batterNumber;
    @BindView(R.id.file_rb_list)
    RecyclerView fileRbList;
    @BindView(R.id.no_message_img)
    ImageView noMessageImg;
    @BindView(R.id.no_message_text)
    TextView noMessageText;
    @BindView(R.id.nothing_text)
    LinearLayout nothingText;
    private ShowFileListAdapter adapter;
    private String TAG = this.getClass().getName();
    private Calendar calendar;
    private ArrayList<ShowFileTypeBean.Content> mPlayList;
    private Disposable subscribe;
    //    private final Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case 1:
//
//                    String timeData = HomeDataUtils.getInstance().getTimeData();
//                    Log.i(TAG, "timeData = " + timeData);
//                    Log.i(TAG, "timeData = " + fragmentTime);
//                    fragmentTime.setText(timeData);
//
//                    new MyTask().execute();
//
//                    break;
//
//            }
//        }
//    };
    private int batteryPower;


    @Override
    public void initData() {
        TimerUtils.schedule("finishAct", new FinishActivityTask(), 3000 * 100);
        EventBus.getDefault().register(this);
        Intent intent = getIntent();
        if (null != intent) {
            ShowFileTypeBean showFileTypeBean = (ShowFileTypeBean) intent.getSerializableExtra("ShowFileTypeBean");
            mPlayList = showFileTypeBean.getFiles();

            adapter = new ShowFileListAdapter(PhoneControlActivity.this, mPlayList);
            fileRbList.setLayoutManager(new LinearLayoutManager(PhoneControlActivity.this));
            fileRbList.setAdapter(adapter);
            fileRbList.addItemDecoration(new DividerItemDecoration(PhoneControlActivity.this, DividerItemDecoration.VERTICAL));

            adapter.notifyDataSetChanged();
            Log.i(TAG, "mPlayList=" + mPlayList);
            Log.i(TAG, "showFileTypeBean = " + showFileTypeBean.toString());

            if (mPlayList.size() == 0) {
                String path = SPUtils.getInstance().getString("pathValue");
                if (path != null) {
                    switch (path) {
                        case "other":
                            noMessageImg.setImageResource(R.drawable.nothing);
                            noMessageText.setText("暂无文件！");
                            break;
                        case "musicplayer":
                            noMessageImg.setImageResource(R.drawable.nomusic);
                            noMessageText.setText("暂无音乐！");
                            break;
                        case "mediaplayer":
                            noMessageImg.setImageResource(R.drawable.novideo);
                            noMessageText.setText("暂无视频！");
                            break;
                        case "imageplayer":
                            noMessageImg.setImageResource(R.drawable.nopic);
                            noMessageText.setText("暂无图片！");
                            break;
                    }
                }
                nothingText.setVisibility(View.VISIBLE);
                fileRbList.setVisibility(View.GONE);
            } else {
                nothingText.setVisibility(View.GONE);
                fileRbList.setVisibility(View.VISIBLE);
            }

        }


    }

   class FinishActivityTask extends TimerTask {
        public void run() {
//            Intent intent = new Intent(PhoneControlActivity.this, IndexActivity.class);
//            intent.putExtra("id", 0);
//            startActivity(intent);
            finish();
        }
    }

    @Override
    public void initView() {
        calendar = Calendar.getInstance();
        String timeData = HomeDataUtils.getInstance().getTimeData();
        String timePeriod = HomeDataUtils.getInstance().getTimePeriod();

        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        int week = calendar.get(Calendar.DAY_OF_WEEK);


        String weekString = "";
        switch (week) {
            case 1:
                weekString = "星期日";
                break;
            case 2:
                weekString = "星期一";
                break;
            case 3:
                weekString = "星期二";
                break;
            case 4:
                weekString = "星期三";
                break;
            case 5:
                weekString = "星期四";
                break;
            case 6:
                weekString = "星期五";
                break;
            case 7:
                weekString = "星期六";
                break;
            default:
                break;
        }

        String dateString = month + "月" + day + "日";
        fragmentDate.setText(dateString);
        fragmentWeek.setText(weekString);
        fragmentMorning.setText(timePeriod);
        fragmentTime.setText(timeData);


//        new MyTask().execute();

        subscribe = Observable.interval(60, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        String timeData = HomeDataUtils.getInstance().getTimeData();
                        Log.i(TAG, "timeData = " + timeData);
                        Log.i(TAG, "timeData = " + fragmentTime);
                        fragmentTime.setText(timeData);
                    }
                });
    }

//    private class MyTask extends AsyncTask {
//
//
//        @Override
//        protected Object doInBackground(Object[] params) {
//
//            handler.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    Message msg = new Message();
//                    msg .what = 1;
//                    handler.sendMessage(msg);
//                }
//            }, 60 * 1000);
//            return null;
//        }
//    }


    @SuppressWarnings("unused")
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onDataChange(DeviceInfoBean bean) {
        DeviceInfoBean.AddressInfo addressInfo = bean.getAddressInfo();
        DeviceInfoBean.WifiInfomation wifiInfomation = bean.getWifiInfomation();
        DeviceInfoBean.GfInfoBean gfInfoBean = bean.getGfInfo();
        int battery = 0;
        String ssid;
        int wifiStrength;

        //设置地理位置
        String ss = HomeDataUtils.getInstance().getAddressInfor(addressInfo);
        fragmentAreas.setText(ss);

        //设置wifi状态
        if (wifiInfomation != null) {
            ssid = wifiInfomation.getSsid();
            wifiStrength = wifiInfomation.getStrength();
            Log.i("DefaultFragment", "ssid=" + ssid);
            wifiSsid.setText(ssid);
            switch (wifiStrength) {
                case 1:
                    wifiShowState.setImageResource(R.drawable.wifi_one);
                    break;
                case 2:
                    wifiShowState.setImageResource(R.drawable.wifi_two);
                    break;
                case 3:
                    wifiShowState.setImageResource(R.drawable.wifi_three);
                    break;
                case 4:
                    wifiShowState.setImageResource(R.drawable.wifi_four);
                    break;

            }
        } else {
            wifiShowState.setImageResource(R.drawable.wifi_disconnect);
        }

        if (gfInfoBean != null) {
            battery = gfInfoBean.getBattery();
            batteryPower = gfInfoBean.getPower();
        }
        int batteryImageResId = HomeDataUtils.getInstance().getbatterStateChange(battery, batteryPower);
        batteryState.setImageResource(batteryImageResId);
        batterNumber.setText(battery * 10 + "%");
    }

    @Override
    public void addViewListener() {

    }

    @Override
    protected boolean shouldInterceptBack() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_phone_control;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
//        handler.removeCallbacksAndMessages(null);
        TimerUtils.close("finishAct");
        subscribe.dispose();
        super.onDestroy();
    }
}
